<template>
  <div class="content">

    <!-- 汇九川 -->
    <div class="left-bottom" v-if="flag">
      <div class="title">
        楼层监控
        <el-tooltip class="item" effect="dark" content="监控列表" placement="bottom" v-if="list.length > 0">
          <el-button class="el_button" @click="gridding"><i class="iconfont dasuolvetuliebiao"></i></el-button>
          <!-- <el-button class="el_button" @click="onRefresh"
            >刷新<i class="iconfont dasuolvetuliebiao"></i
          ></el-button> -->
        </el-tooltip>
      </div>
      <div class="monitoring" v-if="list.length > 0">
        <!-- <div>更多</div> -->
        <div v-for="(item, index) in list" :key="index">
          <!-- @click="videoDetails(item)" -->
          <div class="camara_box" >
            <div :id="`265Player${item.deviceId}`" class="camara_video"></div>
            <div class="deviceId">
              <div class="spaceName">安装点：{{ item.spaceName }}</div>
            </div>
            <!-- <div class="none">加载中</div> -->
          </div>
        </div>
      </div>
      <div v-else style="
          height: 90%;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
        ">
        <img :src="wugj" style="width: 120px; height: 120px; margin-bottom: 10px"/>
        <div>无监控设备</div>
      </div>
    </div>

    <div class="left-top" v-else>
      <div class="title">处理记录</div>
      <div class="fireChief" v-if="tableData !== null">
        <div style="
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
          " v-if="tableData && tableData.length > 0">
          <div class="item" v-for="(item, index) in tableData" :key="index">
            <div class="row">
              <div class="label">
                <el-image class="img" :src="srcAlarm"></el-image>处理人：
              </div>
              <span>{{ item.username }}</span>
            </div>
            <div class="row">
              <div class="label">
                <el-image class="img" :src="srcPerson"></el-image>确认告警类型：
              </div>
              <span>{{ item.alarmFactor }}</span>
            </div>
            <div class="row">
              <div class="label">
                <el-image :src="srcTime" class="img"></el-image>处理时间：
              </div>
              <span>{{ item.handlingTime }}</span>
            </div>
            <div class="row" style="align-items: flex-start">
              <div class="label">
                <el-image :src="srcContent" class="img"></el-image>反馈内容：
              </div>
              <span>{{ item.feedback ? item.feedback : "无" }}</span>
            </div>
            <div class="row" style="align-items: flex-start">
              <div class="label">
                <el-image :src="srcImg" class="img"></el-image>照片：
              </div>
              <el-image v-if="item.pic" :src="item.pic" fit="cover" class="img1" :preview-src-list="srcList"></el-image>
              <span v-else>无</span>
            </div>
          </div>
        </div>
        <div v-else style="height: 90%; display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;">
          <img :src="wugj" style="width: 120px; height: 120px;margin-bottom: 10px;"/>
          <div>无处理记录</div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import { reqGetCamera, reqAlarmHandleResult } from "@/api/api";
import { mapState } from "vuex";

export default {
  components: {},
  data() {
    return {
      srcAlarm: require("@assets/images/datav/record_alarm.png"),
      srcPerson: require("@assets/images/datav/record_person.png"),
      srcTime: require("@assets/images/datav/record_time .png"),
      srcContent: require("@assets/images/datav/record_content.png"),
      srcImg: require("@assets/images/datav/record_img.png"),
      wugj: require("@assets/images/datav/wugj.png"),

      tableData: null,
      srcList: [],

      // 摄像头
      player: null,
      playerEZUIKit: null,
      codec: 1,
      isPause: true,
      token: "",

      list: [],
      t: null,
      i: 0,
      timer: null,

      currentPage: 0,
      totalPage: 0,

      accessToken: "",
      flag: true,
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {},
  methods: {
    gridding() {
      this.$emit("gridding");
    },
    onRefresh() {
      this.i = 0;
      this.playAddress(this.i);
    },
    // 销毁
    getloginOut() {
      this.t &&
        this.t
          .loginOut(this.token)
          .then((res) => {
            // console.log(res);
          })
          .catch((err) => {
            console.log(err);
          });
      if (this.playerEZUIKit) {
        this.destroy()//销毁播放器
      }
    },
    // 销毁
    destroy() {
      var destroyPromise = this.playerEZUIKit.stop();
       destroyPromise = this.playerEZUIKit.destroy();
      destroyPromise.then((data) => {
        console.log("promise 获取 数据", data);
      });
      this.playerEZUIKit = null;

    },
    videoDetails(e) {
      this.$emit("onvideoDetails", e);
    },
    // 安装点查询摄像头
    async GetCamera(spaceId,id) {
      let res = await reqGetCamera(spaceId);
      // if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      // console.log(res, '安装点查询摄像头');
      if (res.data.row.length == 0) {
        this.AlarmHandleResult(id)// 告警处理记录
        this.flag = false;
        return;
      } else {
        this.flag = true;
      }
      this.list = res.data.row || [];
      this.list = this.list.filter((item) => item.classMark == "V380" || item.protocol == "4"); //只添加宏视  ||萤石云
      this.list.forEach(item => {
        if (item.protocol == "4") {
          item.deviceId = item.imei.slice(-4)
        }
      })
      if (this.list.length == 1) {
        this.span = 24;
      }
      // console.log(this.list);
      // await this.init();
      await this.init();
      await this.gettoken()
    },
    // 摄像头
    init() {
      // console.log("摄像头初始化");
      this.t = new Api();
      // console.log(t);
      // 登录
      // t.login('18194005618', 'Yl86503860@').then(res => {
      // t.login('787814981@qq.com', 'qwe123').then(res => {
      this.t
        .login("yuanliu@hongshi.com", "yuanliuhongshi")
        .then((res) => {
          if (res.result == 0) {
            this.token = res.token;
            // 获取设备列表
            this.t
              .getDeviceList(res.token)
              .then((res) => {
                if (res.result == 0) {
                  this.server_ip = res.server_ip;
                  // this.playAddress(this.i);
                  this.getPlay()
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 播放视频
    getPlay() {
      this.list.forEach(async item => {
        if (item.classMark == 'V380') {
          // 宏视
          this.playAddress(item.deviceId)
        }
      })
    },
    // 播放视频
    getPlay2() {
      console.log('萤石云', this.list);
      this.list.forEach(async item => {
        if (item.protocol == '4') {
          console.log('萤石云');
          // 萤石云
          this.getaddress(item)
        }
      })
    },

    // async playAddress(i) {
    //   console.log("摄像头播放地址");
    //   let _this = this;
    //   this.t
    //     .requestPlayAddress(
    //       this.token,
    //       this.list[i].deviceId,
    //       this.server_ip,
    //       1
    //     )
    //     .then((res) => {
    //       if (res.code === 1000) {
    //         _this.codec = res.codec;
    //         _this.play_token = res.play_token;
    //         let width = res.width;
    //         let height = res.height;
    //         this.player = this.t.init(
    //           _this.codec,
    //           _this.server_ip,
    //           _this.play_token,
    //           width,
    //           height
    //         );
    //         if (i < this.list.length - 1) {
    //           this.player.play(`265Player${_this.i}`, true).then(() => {
    //             _this.playAddress(++_this.i);
    //           });
    //         } else {
    //           this.player.play(`265Player${_this.i}`, false);
    //         }
    //       } else {
    //         // console.log(11111);
    //         if (i < this.list.length - 1) {
    //           _this.playAddress(++_this.i);
    //         }
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err);
    //     });
    // },

    async playAddress(deviceId) {
      console.log(deviceId, "deviceId");
      let _this = this;
      this.t
        .requestPlayAddress(
          this.token,
          deviceId,
          this.server_ip,
          0
        )
        .then((res) => {
          if (res.code === 1000) {
            _this.codec = res.codec;
            _this.play_token = res.play_token;
            let width = res.width;
            let height = res.height;
            this.player = this.t.init(
              _this.codec,
              _this.server_ip,
              _this.play_token,
              width,
              height
            );
            this.player.play(`265Player${deviceId}`, true);//播放事件
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 告警处理记录
    async AlarmHandleResult(id) {
      var res = await reqAlarmHandleResult(id);
      // if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.tableData = res.data.row || [];
      this.tableData = this.tableData.slice().reverse();//倒序
      this.srcList = res.data.row
        .filter((item) => item.pic)
        .map((item) => item.pic);
    },


    // 萤石云
    // 获取token
    gettoken() {
      fetch(`https://open.ys7.com/api/lapp/token/get?appKey=d6a1a4287b9a4d78adebaa7bbc8c7125&appSecret=6b9102e6b4ef80beb7067be13d08bd2c`, {
        method: "POST",
      })
        .then(response => response.json())
        .then(res => {
          // console.log(res);
          this.accessToken = res.data.accessToken;
          this.getPlay2()
        });
    },
    // 获取播放地址接口
    getaddress(data) {
      fetch(`https://open.ys7.com/api/lapp/v2/live/address/get?accessToken=${this.accessToken}&deviceSerial=${data.sn}&supportH265=1&quality=2`, {
        method: "POST",
      })
        .then(response => response.json())
        .then(res => {
          // console.log(res, "123213213");
          console.log(this.accessToken, "this.accessToken");
          this.$notify.closeAll();
          if (res.code != 200) return
          //  this.$modal.notifyWarning(res.msg);
          let playerEZUIKit = new EZUIKit.EZUIKitPlayer({
            id: `265Player${data.imei.slice(-4)}`, // 视频容器ID
            accessToken: this.accessToken,
            url: res.data.url,
            useHardDev: true, //开启高性能模式
            template: "44d2c3c3a41a461da5f7a2c925ca1d28",
            // simple - 极简版; pcLive-pc直播；pcRec-pc回放；mobileLive-移动端直播；mobileRec-移动端回放;security - 安防版;voice-语音版;
            plugin: ['talk'], // 加载插件，talk-对讲
            // width: '100%',
            // height: '100%',
          });
          if (playerEZUIKit) {
            this.stopPromiseshow = false;
          }
          this.playerEZUIKit = playerEZUIKit;
        });
    }

  },
  created() {
    // console.log(this.user.tenantId,"this.user.tenantId");
  },
  mounted() { },
};
</script>

<style lang="less" scoped>
.fontSize (@px, @attr: font-size) {
  @vw: (@px / 1920) * 100;
  @{attr}:~"@{vw}vw";
}

.lineHeight (@px, @attr: line-height) {
  @vh: (@px / 1080) * 100;
  @{attr}:~"@{vh}vh";
}

.width (@px, @attr: width) {
  @vw: (@px / 1920) * 100;
  @{attr}:~"@{vw}vw";
}

.height (@px, @attr: height) {
  @vh: (@px / 1080) * 100;
  @{attr}:~"@{vh}vh";
}

.content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  .title {
    .height(43);
    width: 100%;
    margin: 0 auto;
    .fontSize(16);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    background: url("../../../assets/images/datavPop/head.png") no-repeat;
    background-size: 100% 100%;
    font-weight: 600;

    i {
      .fontSize(20);
      color: #fff;
    }
  }

  .left-top {
    height: calc(55% - 1.5vh);
    flex: 1;
    box-shadow: inset -9px 3px 20px 0px #032b60, inset 9px -3px 20px 0px #032e68;
    // margin-bottom: 1.5vh;

    .fireChief {
      height: 93%;
      overflow-y: auto;
      color: #fff;
      overflow-y: auto;
      display: flex;
      flex-direction: column;
      align-items: center;

      .item {
        width: 80%;
        padding: 12px 15px;
        margin-bottom: 10px;
        background: linear-gradient(#062359, rgba(4, 21, 177, 0.38), #061559);
        border-radius: 5px;

        &:first-child {
          margin-top: 10px;
        }

        .row {
          font-size: 0.8vw;
          color: #ffffff;
          margin-bottom: 6px;
          display: flex;
          align-items: flex-end;

          .label {
            color: #8299c9;
            font-size: 0.8vw;

            white-space: nowrap;
            display: flex;
            align-items: center;

            .img {
              width: 15px;
              height: 15px;
              margin-right: 5px;
            }
          }

          .img1 {
            width: 50px;
            height: 40px;
          }
        }
      }
    }
  }

  .left-bottom {
    height: 100%;
    // background: rgba(13, 24, 86, 1);
    box-shadow: inset -9px 3px 20px 0px #032b60, inset 9px -3px 20px 0px #032e68;

    .monitoring {
      height: 90%;
      overflow-y: auto;
      padding: 0 15px;
      box-sizing: border-box;
      margin: 15px 0;

      >div {
        width: 100%;
        height: 30vh;

        .camara_box {
          height: 100%;
          background: black;
          position: relative;
          width: 100%;
          border: 1px solid #073272;
          box-sizing: border-box;

          // 萤石云
          .camara_video {
            z-index: 1;
            width: 100%;
            height: 100%;
          }

          .deviceId {
            width: calc(100% - 20px);
            color: #e9e9e9;
            position: absolute;
            top: 4%;
            padding: 0 10px;
            // display: flex;
            // align-items: center;
            // justify-content: space-between;
            // background:pink;
            flex-wrap: nowrap;
            // right: 4%;
            z-index: 2;

            .spaceName {
              // width: 60%;
              color: #e9e9e9;
              white-space: wrap;
              margin-bottom: 5px;
              // position: absolute;
              // top: 4%;
              // left: 4%;
              // z-index: 2;
            }
          }

          .none {
            color: #cfd3ec38;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 0;
          }
        }
      }
    }
  }
}

::v-deep .el_button.el-button {
  background: transparent !important;
  border: transparent !important;
}
</style>
